$(function () {
    var total = 1;//分页总页面数
    var total_count = 1;//分页总记录数
    var currentPage = 1;//当前页
    var idList = [];//批量选择id所存的数组
    var pageSize = pageOption.pageSize;//每页显示的记录数
    var root_url = $("#root_url").val();

    function init() {
        myPagination();
        bindEvent();
    }

    function bindEvent() {
        $(".inner-section .search-area #search-btn").click(function () {
            render(true, 1, pageSize);
        });

        //enter事件
        $(".inner-section .search-area input").keydown(function (event) {
            event = event ? event : window.event;
            if (event.keyCode == 13) {
                render(true, 1, pageSize);
            }
        });
    }

    /**
     * 案例分页显示方法
     */
    function myPagination() {
        render(true, 1, pageSize);
        //调用公共分页方法
        pagination("#page-selection", {pageSize: pageSize, total: total}, render);

    }

    /**
     * 获取模糊参数
     */
    function getSelectInfo() {
        var selectInfo = {};
        $.each($('#search-param-form').serializeArray(), function (i, field) {
            selectInfo[field.name] = field.value;
        });
        //JSON.stringify(selectInfo);
        return selectInfo;
    }

    /**
     * 分页动态渲染数据
     * @param async ajax请求是否异步
     * @param pageIndex 当前显示页
     * @param pageSize 每页显示记录数
     */
    function render(async, pageIndex, pageSize) {
        var selectInfo = getSelectInfo();
        selectInfo.pageIndex = pageIndex;
        selectInfo.pageSize = pageSize;
        $.ajax({
            async: async,
            type: 'post',
            url: 'pagingGuest',
            data: selectInfo,//从1开始计数
            dataType: 'json',
            success: function (result) {
                var html = '';
                if (result.errorCode == 0) {
                    total = result.data.pageInfo.total_page;
                    total_count = result.data.pageInfo.total_count;
                    $("#page-selection").bootpag({total: total, total_count: total_count});//重新计算总页数,总记录数

                    currentPage = result.data.pageInfo.current_page;
                    var myList = result.data.dataList;

                    html += '<tr>' +
                        '<th class="th1">序号</th>' +
                        '<th class="">头像</th>' +
                        '<th class="">昵称</th>' +
                        '<th class="" style="width: 260px">OpenID</th>' +
                        // '<th class="">省份</th>' +
                        // '<th class="">城市</th>' +
                        // '<th class="">余额</th>' +
                        // '<th class="">累计余额</th>' +
                        '<th class="" style="width: 100px !important;">身份/推荐人</th>' +
                        '<th class="">添加时间</th>' +
                        // '<th class="">操作</th>' +
                        '</tr>';
                    var colspan = $(html).find("th").length;
                    for (var i = 0; i < myList.length; i++) {
                        var obj = myList[i];
                        var num = (pageIndex - 1) * pageSize + i + 1;
                        var head_url = obj.head_img_url;
                        var province = obj.province || '--';
                        var city = obj.city || '--';
                        var phone = obj.phone || '--';
                        var nick_name = obj.nick_name || '--';
                        var identity = obj.identity;
                        var account = obj.account||'--';
                        var add_time = obj.add_time || '--';
                        var balance = obj.balance;
                        var total_balance = obj.total_balance;
                        var sex_text = '未知';
                        var id = obj.id;
                        var member_team_count = obj.member_team_count;
                        var red_mother_team_count = obj.red_mother_team_count;
                        var identity_text = obj.identity_text;//1普通用户2vip会员3红娘4门店

                        var member_team_html = '<a href="javascript:;" style="color: grey;">会员团队</a>'
                        if (member_team_count) {
                            member_team_html = '<a href="memberTeamList?mid=148&pid=' + id + '" data-id="' + id + '" style="color: red;">会员团队</a>';
                        }

                        var red_mother_team_html = '<a href="javascript:;"  style="color: grey;">红娘团队</a>'
                        if (red_mother_team_count) {
                            red_mother_team_html = '<a href="memberTeamList?mid=148&identity=3&pid=' + id + '" data-id="' + id + '" style="color: red;">红娘团队</a>'
                        }

                        html += '<tr>'
                            + '<td class="th1">' + num + '</td>'
                            +'<td>'+(head_url?'<img width="50" src="'+head_url+'">':'无头像')+'</td>'
                            +'<td><span class="limit-text" title="'+nick_name+'">'+nick_name+'</span><br/><span style="color: grey">ID:'+id+'</span></td>'
                            +'<td>'+account+'</td>'
                            // +'<td>'+province+'</td>'
                            // +'<td>'+city+'</td>'
                            // + '<td class="">' + balance + '</td>'
                            // + '<td class="">' + total_balance + '</td>'
                            +'<td>'+identity_text+'</td>'
                            +'<td>'+add_time+'</td>'
                            // + '<td style="text-align: center">'
                            // + '<div>'
                            // + member_team_html
                            // + red_mother_team_html
                            // + '</div>'
                            // + '</td>'
                            + '</tr>';
                    }

                    if (myList.length == 0) {
                        html += '<tr><td colspan="' + colspan + '"><p class="text-danger">暂无数据。</p></td></tr>';
                        $("#list-table tbody").html(html);
                    } else {
                        $("#list-table tbody").html(html);
                        myCheck();
                        batchSelect(idList, ".inner-section #list-table .select-all", ".inner-section #list-table .select-single");
                    }

                } else {
                    responseTip(result.errorCode, result.errorInfo, 1500);
                }
            },
            error: errorResponse
        });
    }

    init();
});